package com.apkicon.test;

import net.dongliu.apk.parser.ApkFile;
import net.dongliu.apk.parser.bean.ApkMeta;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;

/**
 * 检查所有前景资源
 */
public class CheckAllForegroundResources {
    
    public static void main(String[] args) {
        String apkPath = "test.apk";
        
        try (ApkFile apkFile = new ApkFile(apkPath)) {
            System.out.println("🔍 检查所有前景资源: " + apkPath);
            
            // 检查所有可能的前景资源
            String[] possibleForegrounds = {
                "res/-B.png",      // ID: 0x7f080051
                "res/BM.png",      // ID: 0x7f080052
                "res/qD.9.png",    // ID: 0x7f060022 (背景)
            };
            
            for (String resourcePath : possibleForegrounds) {
                System.out.println("\n🔍 检查资源: " + resourcePath);
                
                byte[] data = apkFile.getFileData(resourcePath);
                if (data != null) {
                    System.out.println("✅ 找到文件: " + resourcePath + " (" + data.length + " bytes)");
                    
                    try {
                        BufferedImage image = ImageIO.read(new ByteArrayInputStream(data));
                        if (image != null) {
                            System.out.println("📐 图片尺寸: " + image.getWidth() + "x" + image.getHeight());
                            
                            // 保存图片用于检查
                            String filename = resourcePath.replace("res/", "").replace("/", "_");
                            File outputFile = new File("check_" + filename);
                            ImageIO.write(image, "PNG", outputFile);
                            System.out.println("💾 已保存: " + outputFile.getName());
                            
                            // 判断是否适合作为前景
                            if (image.getWidth() >= 50 && image.getHeight() >= 50) {
                                System.out.println("✅ 尺寸适合作为前景");
                            } else {
                                System.out.println("❌ 尺寸太小，不适合作为前景");
                            }
                        } else {
                            System.out.println("❌ 无法读取图片数据");
                        }
                    } catch (Exception e) {
                        System.out.println("❌ 图片解析失败: " + e.getMessage());
                    }
                } else {
                    System.out.println("❌ 未找到文件: " + resourcePath);
                }
            }
            
        } catch (Exception e) {
            System.err.println("❌ 检查失败: " + e.getMessage());
            e.printStackTrace();
        }
    }
}
